package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.happyscan138.makefun.BaseAdapter.saokuVideo;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.base.VideoPlayView;

import java.util.List;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by xiaok on 2017/1/20.
 */

public class SaokuVideoAdapter extends BaseAdapter implements VideoPlayView.MediaPlayerImpl {
    private List<saokuVideo> ListData;
    private Context mContext;
    private int currentPosition = -1;
    private boolean isPlaying;
    private saokuVideo bean;
    private VideoPlayView playView;
    private View currentItemView;

    public void setSaoKuAddClickListener(SaoKuAddClickListener saoKuAddClickListener) {
        this.saoKuAddClickListener = saoKuAddClickListener;
    }

    private SaoKuAddClickListener saoKuAddClickListener;

    public SaokuVideoAdapter(Context mContext,List<saokuVideo> listDatat) {

        this.mContext = mContext;
       this.ListData = listDatat;
    }
    public SaokuVideoAdapter( ) {

    }
    @Override
    public int getCount() {
        return ListData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder_saoku_video viewHolder;
        //判断converView是否为空
        if(view==null){
            viewHolder=new ViewHolder_saoku_video();
            view=View.inflate(mContext, R.layout.saoku_video_item,null);
            viewHolder.play_btn=(ImageView) view.findViewById(R.id.saoku_play_btn);
            viewHolder.show_layout = (FrameLayout) view.findViewById(R.id.saoku_show_layout);
            viewHolder.play_view = (VideoPlayView) view.findViewById(R.id.saoku_video_play_view);
            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_saoku_video) view.getTag();
        }
        bean=ListData.get(position);
        //点击framlayout传递视频路径
        viewHolder.show_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(saoKuAddClickListener != null) {
                    saoKuAddClickListener.onItemClick(ListData.get(position).video);
                    log.e("onItemAddClick",bean.video);
                }
            }
        });
        //点击播放
        viewHolder.play_btn.setOnClickListener(new MyClick(position,
                viewHolder.play_view, viewHolder.show_layout, view));
        if (currentPosition == position) {
            viewHolder.play_view.setVisibility(View.VISIBLE);

        } else {
            viewHolder.play_view.setVisibility(View.GONE);
            viewHolder.show_layout.setVisibility(View.VISIBLE);
            viewHolder.play_view.stop();
        }
        return view;
    }

    public  interface SaoKuAddClickListener {
        // true add; false cancel
        public void onItemClick(String positionText); //传递boolean类型数据给activity
    }

    public void setPlayView(VideoPlayView playView) {
        this.playView = playView;
        playView.setMediaPlayerListenr(this);
        Log.i("XX", currentPosition + "");
        int[] curr = new int[2];
        currentItemView.getLocationOnScreen(curr);
        Log.i("TAG", curr[1] + "");
    }

    @Override
    public void onError() {

    }

    @Override
    public void onExpend() {

    }

    @Override
    public void onShrik() {

    }

    class MyClick implements View.OnClickListener {
        private int position;
        private VideoPlayView playView;
        private FrameLayout show_layout;
        private View convertView;

        public MyClick(int position, VideoPlayView playView, FrameLayout show_layout, View convertView) {
            this.position = position;
            this.show_layout = show_layout;
            this.playView = playView;
            this.convertView = convertView;
        }

        @Override
        public void onClick(View v) {
            isPlaying = true;
            currentPosition = position;
            show_layout.setVisibility(View.GONE);
            playView.setUrl(bean.video);
            currentItemView = convertView;
            setPlayView(playView);
            playView.openVideo();
            notifyDataSetChanged();
        }
    }
}
class ViewHolder_saoku_video{
    public ImageView play_btn;//播放按钮
    public VideoPlayView play_view;
    public FrameLayout show_layout;
}